package com.lijin.dao;

import com.lijin.dataobject.entity.User;
import java.util.List;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.ResultMap;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

/**
 * Created by gold-jx on 2017/6/2.
 */
@Mapper
public interface UserDao {

    String BASE_RESULT = "userResult";
    String TABLE_NAME = "user";
    String COLUMNS = "id, tb_name, tb_age, tb_sex, tb_password, create_time ";


    @Insert("insert into " + TABLE_NAME + "( tb_name, tb_age, tb_sex, tb_password )" +
            " values " +
            "(#{tbName} ,#{tbAge} ,#{tbSex} ,#{tbPassword} )")
    @Options(useGeneratedKeys = true, keyColumn = "id")
    int insertUser(User user);

    @Delete("DELETE FROM " + TABLE_NAME + " WHERE tb_name=#{name} ")
    int deleteUserByName(String name);

    @Update("UPDATE USER "
            + "SET "
            + "(tb_age=#{tbAge},tb_sex=#{tbSex} ,'tb_password'={tbPassword} )"
            + "WHERE "
            + "tb_name=#{tbName}"
    )
    int updateUserByName(User user);

    @Select("Select " + COLUMNS + " from " + TABLE_NAME + " where tb_name=#{name}")
    @Results(
            id = BASE_RESULT,
            value = {
                    @Result(property = "id", column = "id"),
                    @Result(property = "tbName", column = "tb_name"),
                    @Result(property = "tbAge", column = "tb_age"),
                    @Result(property = "tbSex", column = "tb_sex"),
                    @Result(property = "tbPassword", column = "tb_password"),
                    @Result(property = "createTime", column = "create_time"),
            }
    )
    User findUserByName(String name);

    @Select("Select " + COLUMNS + " from " + TABLE_NAME)
    @ResultMap(BASE_RESULT)
    List<User> findUserAll();


}
